package es.gesfutbol.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import es.gesfutbol.domini.implementacio.CombinacioGeneral;
import es.gesfutbol.domini.implementacio.CombinacioPropia;
import es.gesfutbol.domini.implementacio.PartitDeQuiniela;
import es.gesfutbol.domini.implementacio.Pronostic;
import es.gesfutbol.util.HibernateDao;
import es.gesfutbol.util.HibernateHelper;

public class PronosticDao extends HibernateDao<Pronostic, Long> implements IPronosticDao{
	
	@Override
	@Transactional(readOnly=false)
	public void saveOrUpdate(Pronostic pronostic) {
		this.getSessionFactory().getCurrentSession().saveOrUpdate(pronostic);
	}
	
	@Transactional(readOnly = true)
	public Pronostic findByCombinacioPropiaIPartitQuinielaIPenyista(Long idCombinacioPropia, Long idPartitQuiniela,Long idPenyista){
		Pronostic p = (Pronostic) this.getSessionFactory().getCurrentSession()
			.createCriteria(Pronostic.class)
			.add( Restrictions.eq("partitDeQuiniela.idPartitDeQuiniela", idPartitQuiniela) )
			.add( Restrictions.eq("penyista.idPenyista", idPenyista) )		
			.add( Restrictions.eq("combinacioPropia.idCombinacioPropia", idCombinacioPropia) )
			.uniqueResult();		
		return p;
	}
	
	@Transactional(readOnly = true)
	public Pronostic findByCombinacioGeneralIPartitQuinielaIPenyista(Long idCombinacioGeneral,Long idPartitQuiniela,Long idPenyista){
		Pronostic p = (Pronostic) this.getSessionFactory().getCurrentSession()
		.createCriteria(Pronostic.class)
		.add( Restrictions.eq("partitDeQuiniela.idPartitDeQuiniela", idPartitQuiniela) )
		.add( Restrictions.eq("penyista.idPenyista", idPenyista) )
		.add( Restrictions.eq("combinacioGeneral.idCombinacioGeneral", idCombinacioGeneral) )	
    	.uniqueResult();		
		return p;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Pronostic> findByPartitQuinielaIPenyista(Long idPartitQuiniela, Long idPenyista){
		List<Pronostic> pronostics = this.getSessionFactory().getCurrentSession()
			.createCriteria(Pronostic.class)
			.add( Restrictions.eq("partitDeQuiniela.idPartitDeQuiniela", idPartitQuiniela) )
			.add( Restrictions.eq("penyista.idPenyista", idPenyista) )			
			.list();		
		return pronostics;
	}
	
	@Transactional(readOnly = true)
	public Pronostic findByPartitQuinielaIAdministrador(Long idPartitQuiniela, Long idPenyista){
		Pronostic pronostic = (Pronostic)this.getSessionFactory().getCurrentSession()
			.createCriteria(Pronostic.class)
			.add( Restrictions.eq("partitDeQuiniela.idPartitDeQuiniela", idPartitQuiniela) )
			.add( Restrictions.eq("penyista.idPenyista", idPenyista) )			
			.uniqueResult();		
		return pronostic;
	}
	
	@SuppressWarnings("deprecation")
	@Transactional(readOnly = true)
	public Set<Pronostic> deproxyCombinacions(Set<Pronostic> pronostics){
		
		Iterator<Pronostic> itPro = pronostics.iterator();
		Set<Pronostic> pronostics2 = new HashSet<Pronostic>();
		while (itPro.hasNext()) {
			Pronostic p = itPro.next();
			this.getSessionFactory().getCurrentSession().lock(p, LockMode.NONE);
			CombinacioGeneral cG = p.getCombinacioGeneral();
			CombinacioPropia cP = p.getCombinacioPropia();
			Hibernate.initialize(cG);
			cG = HibernateHelper.deproxy(cG, CombinacioGeneral.class);
			p.setCombinacioGeneral(cG);
			Hibernate.initialize(cP);
			cP = HibernateHelper.deproxy(cP, CombinacioPropia.class);
			p.setCombinacioPropia(cP);			
			pronostics2.add(p);
		}		
		return pronostics2;			
	}
		
	@SuppressWarnings("deprecation")
	@Transactional(readOnly = true)
	public List<Pronostic> deproxyPDQ(List<Pronostic> pronostics){		
		Iterator<Pronostic> itPro = pronostics.iterator();
		List<Pronostic> pronostics2 = new ArrayList<Pronostic>();
		while (itPro.hasNext()) {
			Pronostic p = itPro.next();
			this.getSessionFactory().getCurrentSession().lock(p, LockMode.NONE);
			PartitDeQuiniela pDQ = p.getPartitDeQuiniela();			
			Hibernate.initialize(pDQ);
			pDQ = HibernateHelper.deproxy(pDQ, PartitDeQuiniela.class);
			p.setPartitDeQuiniela(pDQ);					
			pronostics2.add(p);
		}		
		return pronostics2;			
	}

}
